//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: [{
        'title': '线上课程',
        'active': true,
        'method': 'GetOnlineCoursesList'
      },
      {
        'title': '线下课程',
        'active': false,
        'method': 'GetoffLineCourseList'
      }
    ],
    // 
    uid: "",
    imgUrl: baseJs.imgUrl,
    pageIndex: 1,
    state: "",
    courseList: [],
    method: "GetOnlineCoursesList"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.GetOnlineCoursesList();
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.data.pageIndex++;
    this.GetOnlineCoursesList();
  },
  topTap: function(e) {
    var top = this.data.top;
    var method = e.currentTarget.dataset.method;
    for (var i = 0, len = top.length; i < len; ++i) {
      if (top[i].method == method) {
        top[i].active = true;
      } else {
        top[i].active = false
      }
    }
    this.setData({
      top: top,
      method: method,
      pageIndex: 1,
      courseList: []
    })
    if (method == "GetoffLineCourseList") {

    } else {

      this.GetOnlineCoursesList();
    }
  },
  //
  GetOnlineCoursesList: function() {
    var that = this;
    var pageIndex = this.data.pageIndex;
    var courseList = this.data.courseList;
    var method = this.data.method;
    if (pageIndex == 1) {
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=" + method + "&PageSize=10&PageIndex=" + pageIndex + "&IsAsc=true&OrderBy=CourseMoney&CourseTypeId=0&SearchKey="),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          courseList.push(json[i])
        }
        that.setData({
          courseList: courseList,
        })
        // console.log(courseList);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();
      }
    })
  },
})